﻿ $(function(){
            
            var functionList=$('#functionList');
            var selectedList=$('#selectedFunction');
            //加载已选择的功能
            var idlist= $('[id$=hfSelectedFunIdList]').val();
            var list=idlist.split(',');
            for(var i=0;i<list.length;i++)
            {
                if(list[i]!='')
                {
                    var id=list[i];
                    var name=$('#'+id).next('label').text();
                    showSelectedFunction(id,name,selectedList);
                }
            }
            //搜索
            functionList.find('.search').keyup(function(){
                var param=$.trim(this.value);
                functionList.find('label').each(function(){
                    if(param!="")
                    {
                        if($(this).text().indexOf(param)!=-1)
                        {
                            $(this).css('background-color','yellow');
                        }
                        else
                        {
                            $(this).css('background-color','');
                        }
                    }
                    else
                    {
                        $(this).css('background-color','');
                    }
                });
            });
            
            //选择
           functionList.find('.fun').click(function(){
                var id=$(this).attr('id');
                var funName=$(this).next('label').text();
                if($(this).attr('checked'))
                {
                    showSelectedFunction(id,funName,selectedList);
                }
                else
                {
                    removeSelectedFunction(id);
                }
           }); 
           
           
           //对表的显示和隐藏
           var isPair=$('[id$=cbxIsPair]');
           var pairCode=$('[id$=txtPairMerCode]');
           if(isPair.attr('checked'))
           {
                pairCode.removeAttr('disabled');
           }
           else 
           {
                pairCode.attr('disabled','disabled');
           }
           isPair.change(function(){
                if($(this).attr('checked'))
                {
                    pairCode.removeAttr('disabled');
                }
                else{
                    pairCode.attr('disabled','disabled');    
                }    
           });
           
           // WdatePicker
           $('[id$=txtAddDate]').focus(function(){
                WdatePicker(
                {
                   isShowWeek:true
                })
           });
           
        });
        
        function showSelectedFunction(id,name,target)
        {
            var funID="fun_"+id;
            var delImg=$('<img>').attr('src',"/Backend/images/delete.gif").click(function(){
                    removeSelectedFunction(id);
                });
                
            $('<span>').attr('id',funID).addClass('selected').text(name).append(delImg).appendTo(target);
            funCheck(id,true);
            getSelectedFunIdList();
        }
        function removeSelectedFunction(id)
        {
            var funID="fun_"+id;
            $('#'+funID).remove();
            funCheck(id,false);
            getSelectedFunIdList();
        }
        function getSelectedFunIdList(target)
        {
            var idlist='';
            $('#selectedFunction span.selected').each(function(){
                idlist+=this.id.substring(4,this.id.length)+',';
            });
            if(idlist.length!=0)
            {
                idlist=idlist.substring(0,idlist.length-1);
            }
            $('[id$=hfSelectedFunIdList]').val(idlist);
            
            $('#idlist').text(idlist);
        }
        function funCheck(funId,checked)
        {
            $('#'+funId).attr('checked',checked).next('label').css('font-weight',checked===true?'bold':'');
        }